package com.me.screen;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.scenes.scene2d.Group;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.ui.ImageButton;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable;
import com.me.constant.Constant;
import com.me.control.CreateWindown;
import com.me.enums.ScreenEnum;
import com.me.helper.PreferencesData;
import com.me.memorytest.MainMemoryTest;
import com.me.ui.HeaderGame;

public class MainMenuScreen extends BaseScreen {

	Image img_bg, img_bgTop, bg_subMenu;
	static ImageButton img_back;
	Label lb_title;
	TextButton btn_NewGame;
	TextButton btn_Continue;
	TextButton btn_Category;
	TextButton btn_Custom;
	Image img_sound;
	ImageButton img_highScore;
	CreateWindown cr = new CreateWindown();
	Group groupMenu = new Group();
	
	@Override
	public void show() {
		super.show();
		
		stage.clear();
		
		Constant.b_custom = false;
		img_bg = new Image(MainMemoryTest.getAtlas().findRegion("datagame/bg_menuGame"));
		stage.addActor(img_bg);
		
		img_bgTop = new Image(Constant.atlas.findRegion("datagame/menu-bottom-thanh-ngang"));
		img_bgTop.setBounds(0, Constant.STAGE_HEIGHT-70, Constant.STAGE_WIDTH, 70);
		stage.addActor(img_bgTop);
		
		TextureRegionDrawable bup = new TextureRegionDrawable(Constant.atlas.findRegion("datagame/Back_up"));
		TextureRegionDrawable bdown = new TextureRegionDrawable(Constant.atlas.findRegion("datagame/Back_down"));
		img_back = new ImageButton(bup, bdown);
		img_back.setX(20);
		img_back.setY(Constant.STAGE_HEIGHT- img_bgTop.getHeight() + (img_bgTop.getHeight() - img_back.getHeight())/2);
		img_back.addListener(new ClickListener(){
			@Override
			public void touchUp(InputEvent event, float x, float y,
					int pointer, int button) {
				super.touchUp(event, x, y, pointer, button);
				stage.addActor(CreateWindown.confirmExit());
			}
		});
		stage.addActor(img_back);
		
		bg_subMenu = new Image(Constant.atlas.findRegion("datagame/submenu_panel"));
		stage.addActor(bg_subMenu);
		
		
		lb_title = new Label("CHOOSE YOUR OPTION", Constant.skin,"labelMenu");
		lb_title.setX(Constant.STAGE_WIDTH/2 - lb_title.getWidth()/2);
		lb_title.setY(Constant.STAGE_HEIGHT - img_bgTop.getHeight() + (img_bgTop.getHeight() - lb_title.getHeight())/2);	
		stage.addActor(lb_title);
		
		btn_NewGame = new TextButton("New game", Constant.skin, "textButton_45_bold_Menu");
		btn_NewGame.setX(440);
		btn_NewGame.setWidth(400);
		btn_NewGame.setHeight(100);
		btn_NewGame.addListener(new ClickListener(){
			@Override
			public void clicked(InputEvent event, float x, float y) {
				super.clicked(event, x, y);
				if(PreferencesData.getLevel() > 1){
					BaseScreen.stage.addActor(HeaderGame.img_bgHide);
					BaseScreen.stage.addActor(CreateWindown.createWindow_ConfirmNewGame());
				}else{
					Constant.LEVEL = 1;
					Constant.iCountClicked = 0;
					PreferencesData.setLevel(Constant.LEVEL);
					PreferencesData.setCountClicked(Constant.iCountClicked);
					HeaderGame.lb_Click.setText("Clicked: 0");
					MainMemoryTest.getScreenManager().createScreen(ScreenEnum.PLAY_SCREEN);
					MainMemoryTest.getScreenManager().getScreen().show();
				}
			}
			
		});
		groupMenu.addActor(btn_NewGame);
		
		btn_Continue = new TextButton("Continue", Constant.skin, "textButton_45_bold_Menu");
		btn_Continue.setX(440);
		btn_Continue.setWidth(400);
		btn_Continue.setHeight(100);
		btn_Continue.addListener(new ClickListener() {
			@Override
			public void clicked(InputEvent event, float x, float y) {
				super.clicked(event, x, y);
				Constant.LEVEL = PreferencesData.getLevel();
				Constant.iCountClicked = PreferencesData.getCountClicked();
				HeaderGame.lb_Click.setText("Clicked: " + Constant.iCountClicked);
				MainMemoryTest.getScreenManager().createScreen(ScreenEnum.PLAY_SCREEN);
				MainMemoryTest.getScreenManager().getScreen().show();
			}
		});
		
		btn_Category = new TextButton("Category", Constant.skin, "textButton_45_bold_Menu");
		btn_Category.setX(440);
		btn_Category.setWidth(400);
		btn_Category.setHeight(100);
		btn_Category.addListener(new ClickListener(){
			@Override
			public void clicked(InputEvent event, float x, float y) {
				super.clicked(event, x, y);
				stage.addActor(CreateWindown.createCategory());
			}
			
		});
		groupMenu.addActor(btn_Category);
		
		btn_Custom = new TextButton("Custom", Constant.skin, "textButton_45_bold_Menu");
		btn_Custom.setX(440);
		btn_Custom.setWidth(400);
		btn_Custom.setHeight(100);
		btn_Custom.addListener(new ClickListener(){
			@Override
			public void clicked(InputEvent event, float x, float y) {
				super.clicked(event, x, y);
				Constant.b_custom = true;
				BaseScreen.stage.addActor(CreateWindown.createWindow_CustomCards());
			}
			
		});
		groupMenu.addActor(btn_Custom);
		
		if(PreferencesData.getLevel() > 1){
			btn_NewGame.setY(500);
			btn_Continue.setY(btn_NewGame.getY() - 130);
			btn_Category.setY(btn_Continue.getY() - 130);
			btn_Custom.setY(btn_Category.getY() - 130);
			groupMenu.addActor(btn_Continue);
		}
		else{
			btn_NewGame.setY(450);
			btn_Category.setY(btn_NewGame.getY() - 130);
			btn_Custom.setY(btn_Category.getY() - 130);
		}
		
		img_sound = new Image(Constant.atlas.findRegion("datagame/sound"));
		img_sound.setX(40);
		img_sound.setY(140);
		if(PreferencesData.getBoolRememberSound())
			img_sound.setDrawable(new TextureRegionDrawable(Constant.atlas.findRegion("datagame/sound")));
		else
			img_sound.setDrawable(new TextureRegionDrawable(Constant.atlas.findRegion("datagame/sound_mute")));
		img_sound.addListener(new ClickListener(){
			@Override
			public void clicked(InputEvent event, float x, float y) {
				super.clicked(event, x, y);
				if(PreferencesData.getBoolRememberSound()){
					img_sound.setDrawable(new TextureRegionDrawable(Constant.atlas.findRegion("datagame/sound_mute")));
					PreferencesData.setBoolRememberSound(false);
				}
				else{
					img_sound.setDrawable(new TextureRegionDrawable(Constant.atlas.findRegion("datagame/sound")));
					PreferencesData.setBoolRememberSound(true);
				}
			}
		});
		groupMenu.addActor(img_sound);
		
		TextureRegionDrawable hUp = new TextureRegionDrawable(Constant.atlas.findRegion("datagame/cup"));;
		TextureRegionDrawable hDown = new TextureRegionDrawable(Constant.atlas.findRegion("datagame/cup_press"));;
		img_highScore = new ImageButton(hUp, hDown);
		img_highScore.setX(40);
		img_highScore.setY(20);
		img_highScore.addListener(new ClickListener(){
			@Override
			public void clicked(InputEvent event, float x, float y) {
				super.clicked(event, x, y);
				Constant.b_HighScoreHome = true;
				MainMemoryTest.getScreenManager().createScreen(ScreenEnum.HIGH_SCORE_SCREEN);
				MainMemoryTest.getScreenManager().getScreen().show();
			}
		});
		groupMenu.addActor(img_highScore);
		
		stage.addActor(groupMenu);
	}

	
	@Override
	public void render(float arg0) {
		super.render(arg0);
		Gdx.gl.glClearColor(0, 0, 0.2f, 1);
		Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);

		// tell the camera to update its matrices.
		camera.update();

		// tell the SpriteBatch to render in the coordinate system specified by the camera.
		sb.setProjectionMatrix(camera.combined);

		// begin a new batch and draw the bucket and all drops
		sb.begin();
		sb.end();

		if(Gdx.input.isKeyPressed(Keys.BACK)){
			stage.addActor(CreateWindown.confirmExit());
		}
		stage.setViewport(Constant.STAGE_WIDTH, Constant.STAGE_HEIGHT,false);
	}

	@Override
	public void pause() {
		// TODO Auto-generated method stub
		super.pause();
	}

	@Override
	public void resize(int width, int height) {
		super.resize(width, height);
		super.resize(width, height);
		width = 1280 * width / height;
		height = 720;
		BaseScreen.stage.setViewport(width, height, false);
	}

	@Override
	public void resume() {
		// TODO Auto-generated method stub
		super.resume();
	}
}
